#Health of Online Communities
#
#
#Separates data into files by namespace, and then gathers statistics

import sys
import os
import collections


def main():
	#global fileList #list of prelim files sorted by namespace	
	#fileList = []
	
	createFiles()

	

#separates data into initial files of like namespaces
def createFiles():
        path = sys.argv[1]
        inputFileList = os.listdir(path)

        openDict = {}
        for inputFileName in inputFileList:
	        inputFilePath = path+inputFileName
		f = open(inputFilePath) #opens file 

		for line in f:
			pieces = line.split('\t')
			namespace = pieces[2].strip() # obtains namespace
			fileName = namespace+".txt" #makes a filename 

			if fileName not in openDict:
				fileVar = open("results/"+fileName,"a")
				openDict[fileName] = fileVar 
			else:
				fileVar = openDict[fileName]
			 
			fileVar.write(line) #writes the line in the corresponding namespace file

	for fileVar in openDict.values(): 	
		fileVar.close()	

main()
